<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="120px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="业务员名称" prop="salesmanName">
        <el-input
          v-model="queryParams.salesmanName"
          placeholder="请输入业务员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目地址" prop="projectAddress">
        <el-input
          v-model="queryParams.projectAddress"
          placeholder="请输入项目地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程部人员姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入工程部人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:cost:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:cost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:cost:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:cost:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键id" align="center" prop="id" />-->
<!--      <el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
<!--      <el-table-column label="业务员id" align="center" prop="salesman" />-->
      <el-table-column label="业务员名称" align="center" prop="salesmanName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目地址" align="center" prop="projectAddress" />
<!--      <el-table-column label="工程部人员id" align="center" prop="userId" />-->
      <el-table-column label="工程部人员姓名" align="center" prop="userName" />

      <el-table-column label="工程部人员照片" align="center" prop="userPhoto"  width="150">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" :src="`http://localhost/dev-api/${scope.row.userPhoto}`">
            <div slot="error" class="image-slot" style="line-height: 50px">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="出差天数" align="center" prop="days" />
      <el-table-column label="费用" align="center" prop="costs" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:cost:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:cost:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body @open="openDialog">
      <el-form ref="form" inline :model="form" :rules="rules" label-width="140px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input style="width:200px" v-model="form.orderNo">
            <template slot="append">
              <el-button style="width:60px" type="primary" @click="orderListFn">选择</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="业务员名称" prop="salesmanName">
          <el-input v-model="form.salesmanName" placeholder="请输入业务员名称" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目地址" prop="projectAddress">
          <el-input v-model="form.projectAddress" placeholder="请输入项目地址" />
        </el-form-item>
        <el-form-item label="工程部人员姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入工程部人员姓名" />
        </el-form-item>
        <br/>
        <el-form-item label="工程部人员照片" prop="userPhoto">
          <image-upload v-model="form.userPhoto"/>
        </el-form-item>
        <br>
        <el-form-item label="出差天数" prop="days">
          <el-input v-model="form.days" placeholder="请输入出差天数" />
        </el-form-item>
        <el-form-item label="费用" prop="costs">
          <el-input v-model="form.costs" placeholder="请输入费用" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 租赁订单选择框-->
    <el-dialog
      title="请选择租赁订单"
      :visible.sync="orderListIsShow"
      width="50%"
      @close="OrderListClose">
      <el-form :model="OrderFormData" inline  label-width="100px" >
<!--        <el-form-item label="客户姓名" prop="customerName">-->
<!--          <el-input style="width: 240px" v-model="OrderFormData.customerName" placeholder="请选择客户名" >-->
<!--            <template slot="append">-->
<!--              <el-button type="primary" @click="openProject">-->
<!--                选择客户-->
<!--              </el-button>-->
<!--            </template>-->
<!--          </el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="项目名称" prop="projectName">-->
<!--          <el-input style="width: 240px" v-model="OrderFormData.projectName" placeholder="请输入项目名称" >-->
<!--            <template slot="append">-->
<!--              <el-button type="primary" @click="openProject">-->
<!--                选择项目-->
<!--              </el-button>-->
<!--            </template>-->
<!--          </el-input>-->
<!--        </el-form-item>-->

        <el-form-item label="订单状态" prop="orderStatus">  <el-select v-model="OrderFormData.orderStatus" placeholder="请选择订单状态">
          <el-option
            v-for="item in OrderOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select></el-form-item>
      </el-form>
      <el-button style="margin-bottom: 15px;margin-left: 100px;" size="small" type="success" @click="SearchOrderNo">查询</el-button>
      <el-table :data="OrderList" tooltip-effect="dark" style="width: 100%">
        <el-table-column align="center" label="订单编号" prop="orderNo" width="150"></el-table-column>
        <el-table-column align="center" prop="customerName" label="客户名称" width="120"></el-table-column>
        <el-table-column align="center" prop="projectName" label="项目名" width="120"></el-table-column>
        <el-table-column align="center" prop="salesmanName" label="业务员" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" prop="orderDate" label="订单创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="checked(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listCost, getCost, delCost, addCost, updateCost } from "@/api/module/cost";
import {listOrder} from "@/api/module/order";
import log from "@/views/monitor/job/log";
import {getLoginUser} from "@/api/system/user"
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "Cost",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        salesman: null,
        salesmanName: null,
        projectName: null,
        projectAddress: null,
        userId: null,
        userName: null,
        userPhoto: null,
        days: null,
        costs: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      orderListIsShow:false,
      OrderList:[],
      OrderOptions:[],
      OrderFormData:{
        orderStatus:'2'
      }
    };
  },
  created() {
    this.getList();
    this.getOrderOptions()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listCost(this.queryParams).then(response => {
        this.costList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取租赁订单状态
    async getOrderOptions(){
      let res=await getDicts('order_type')
      this.OrderOptions=res.data
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        salesman: null,
        salesmanName: null,
        projectName: null,
        projectAddress: null,
        userId: null,
        userName: null,
        userPhoto: null,
        days: null,
        costs: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCost(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delCost(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/cost/export', {
        ...this.queryParams
      }, `cost_${new Date().getTime()}.xlsx`)
    },
    async orderListFn(){
      let res= await listOrder(this.OrderFormData)
      // console.log(res)
      this.OrderList=res.rows
      this.orderListIsShow=true

    }, async openDialog(){
      let res=await getLoginUser()
      console.log(res)
      this.form.userId=res.user.userId
      this.form.userName=res.user.username
    },
    checked(val){
      console.log(11)
      console.log(val)
      this.form.orderNo=val.orderNo
      this.form.salesmanName=val.salesmanName
      this.form.salesman=val.salesman
      this.form.projectName=val.projectName
      this.form.projectAddress=val.address
      this.orderListIsShow=false
    },
    OrderListClose(){
      // this.orderListIsShow=false
      this.OrderList=[]

    }
  }
};
</script>
